package com.woniuxy.gateway.controller;


import com.woniuxy.cloud.demo.Result;
import com.woniuxy.cloud.exception.BatteryException;
import com.woniuxy.cloud.util.CommonsUtil;
import com.woniuxy.gateway.entity.AuthUserDetails;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/activeCode")
public class ActiveCodeController {
    @Resource
    private StringRedisTemplate srt;

    /**
     * 验证码
     *
     * @return
     */
    @PostMapping("captcha.do/{phone}")
    public Result captcha(@PathVariable String phone) {
        //获取6位随机验证码
        String randomString = new CommonsUtil().getRandomString(6);


        srt.opsForValue().set("shared:battery:activeCode:phone:" + phone, randomString);
        //设置过期时间
        srt.expire("shared:battery:activeCode:phone:" + phone, 2, TimeUnit.HOURS);


        System.out.println("验证码!!!!!!!!!!!!!!" + randomString);

        return Result.newInstance().setCode(200).setMsg("验证码发送成功");
    }
}
